<layout name="layout" />

<div class="pageheader notab">
   <h1 class="pagetitle" id="campaign_title">正在初始化的自动开车宝贝</h1>
    <ul class="breadcrumbs breadcrumbs2"  >
        <li><a href="/">首页 / 全部推广计划</a><span class="divider">&#187;</span></li>
        <li><a href="/adgroup">全部推广宝贝</a><span class="divider">&#187;</span></li>
        <li><a href="/adgroup?type=auto">自动开车的宝贝</a><span class="divider">&#187;</span></li>
        <li>正在初始化的自动开车宝贝</li>
    </ul>
</div>
  
 <div id="contentwrapper" class="contentwrapper">
	
  <div id="auto_status_wrapper">
    <h4>正在进行自动推广的宝贝</h4>
    <empty name="unfinished_adgroups">
    <div class="info">当前没有正在进行推广的宝贝</div>
    <else />
    <table cellspacing="0" cellpadding="0" border="0" class="metrotable" id="table1">
            <colgroup>
                <col class="con1" style="width:40%;">
          <col class="con0" style="width:20%;">
          <col class="con1" style="width:15%;">
          <col class="con0" style="width:25%;">
      </colgroup>
      <thead>
          <tr>
              <th class="head1">推广宝贝</th>
              <th class="head0">当前状态</th>
              <th class="head1">目前进度</th>
              <th class="head0">操作</th>
          </tr>
      </thead>

      <tbody>
          <volist name="unfinished_adgroups" id="v">
          <tr id="product_{$v.product_id}_{$v.campaign_id}" rel="<if condition='($v.status eq 0) OR ($v.status eq 1)'>{$v.product_id}_{$v.campaign_id}</if>" >
              <td>
            <ul class="listview">
            <li style="cursor:auto;margin-bottom:0">
                <div class="icon">
                    <img src="{$v.img_url1}">
                </div>

                <div class="data">
                    <h4>{$v.title1}</h4>
                    <div class="filed">
                    	所属推广计划: {$v.campaign_title}
                    </div>
                </div>
            </li>
              </ul>
              </td>
              
              <td class="auto_status">
              	<if condition="$v.status eq 0">
              		<span class="label label-info">正在等待中</span>
              	</if>
              	
               	<if condition="$v.status eq 1">
              		<span class="label label-info">正在智能分析中</span>
              	</if>
              	
              	<if condition="$v.status eq 2">
              			<span class="label label-success">智能分析完成<if condition="$v.need_confirm eq 1">, 等待确认提交<else/>, 已在推广中</if> </span>
              	</if>
              	
                <if condition="$v.status eq -1">
                	<span class="label label-important">失败: <if condition="$v.msg eq 'Invalid arguments:title'">标题无效<else/>{$v.msg|strip_tags}</if></span>
                	
                </if>
              </td>
              
              <td>
              	<div class="progress" style="width:70%;">
                      <div class="bar"><div class="value bluebar" style="width: {$v.progress}%;"></div></div>
                  </div>
              </td>
              <td class="center" id="oper_{$v.product_id}_{$v.campaign_id}">
                  	<if condition="$v.status eq 2">
	                  	&nbsp;<a class="actionBtn" title="查看该推广" href="/adgroup/manage?adgroup_id={$v.adgroup_id}">查看</a>
	                  	<if condition="$v.need_confirm eq 1">
		                  	&nbsp;<a class="actionBtn" title="确认该推广" href="javascript:wt.confirm_auto({$v.product_id},{$v.campaign_id})">确认</a>
		                    &nbsp;<a class="actionBtn" title="删除该推广" href="javascript:wt.del_auto({$v.product_id},{$v.campaign_id})">删除</a>
		                </if>
                    </if>
                    <if condition="$v.status eq -1"> 
                        <a class="actionBtn" title="删除该推广" href="javascript:wt.del_auto({$v.product_id},{$v.campaign_id})">删除</a>
                     </if>
              </td>
          </tr>
          </volist>
      </tbody>
    </table>
    </empty>
    <h4 style="padding-top:30px">最近24小时内提交并且完成的自动推广宝贝</h4>
    <empty name="finished_adgroups">
    <div class="info">最近24小时内您没有<notempty name="unfinished_adgroups">其他</notempty>自动推广宝贝</div>
    <else />
    <table cellspacing="0" cellpadding="0" border="0" class="metrotable" id="table2">
       <colgroup>
           <col class="con1" style="width:40%;">
           <col class="con0" style="width:20%;">
           <col class="con1" style="width:15%;">
           <col class="con0" style="width:25%;">
       </colgroup>
       <thead>
           <tr>
               <th class="head1">推广宝贝</th>
               <th class="head0">当前状态</th>
               <th class="head1">完成进度</th>
               <th class="head0">操作</th>
           </tr>
       </thead>

       <tbody>
           <volist name="finished_adgroups" id="v">
           <tr id="product_{$v.product_id}_{$v.campaign_id}">
               <td>
                   <ul class="listview">
                       <li style="cursor:auto;margin-bottom:0">
                           <div class="icon">
                               <img src="{$v.img_url1}">
                           </div>
  
                           <div class="data">
                               <h4>{$v.title1}</h4>
                               <div class="filed">所属推广计划: {$v.campaign_title}</div>
                           </div>
                       </li>
                  </ul>
               </td>
               
               <td>
                    <if condition="$v.msg neq NULL ">
              	    	<span class="label label-warning">{$v.msg}</span>
              	    <else/>
                   <span class="label label-success">智能分析完成<if condition="$v.need_confirm eq 1">, 等待确认提交<else/>, 已在推广中</if> </span>
                   </if>
               </td>
               
               <td>
                   <div class="progress" style="width:70%;">
                       <div class="bar"><div class="value bluebar" style="width: 100%;"></div></div>
                   </div>
               </td>
               <td class="center">
                   <div>
                       <a class="actionBtn" title="查看该推广" href="/adgroup/manage?adgroup_id={$v.adgroup_id}">查看</a>
                   </div>
               </td>
           </tr>
           </volist>
       </tbody>
    </table>
    </empty>
    <div class="info" style="padding-top:30px">
        <a href="/adgroup?type=auto">点此查看所有成功自动推广的宝贝</a>.<br/> 
                温馨提示: 您也可以点击左边菜单栏 全部推广宝贝 -&gt; 自动开车 查看所有自动开车的宝贝. 
    </div>
  </div>
</div>
     
<!-- centercontent -->

<script type="text/javascript">
	$(".metrotable tbody tr").hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
	
	/* $('.hiddenaction').find('a').bind('click' , function(){
		var url = $(this).attr('href');
		var $this_tr = $(this).parents('tr');
		wt.get(url , '' ,function(r){
			var o = $.parseJSON(r);
			if(o.status == 200 && o.data.result){
				$this_tr.fadeOut('slow' , function(){
					$(this).remove();
				});
			}else{
				$.jGrowl( o.data.msg );
			}
		});
		return false;
	}); */
	
	var timer = setInterval(function(){
		var unfinished_items = new Array();
		$('#table1 tbody tr').each(function() {if($(this).attr('rel') != '') unfinished_items.push($(this).attr('rel'))});
		unfinished_items = unfinished_items.join(',');
		if(unfinished_items == '') { // stop when every one is finished
			window.clearInterval(timer);
			return;
		}
		var url = '/adgroup/auto?items=' + unfinished_items;
		wt.get(url , '' , function(r){
			var o = $.parseJSON(r);
			if(o.data.result){
				for(ix in o.data.products){
					var p = o.data.products[ix];
					var $tr = $('#product_' + p.product_id + '_' + p.campaign_id);
					
					if( $tr ){
						var status_td = $tr.find(".auto_status");
						var progress = $tr.find('.progress .value');
						switch(p.status){
							case "-1":
								status_td.html( '<span class="label label-important">失败:' +p.msg+ '</span>' )  ;
								progress.css( {'width':p.progress + '%'} );
								var oper_btns = '<a class="actionBtn" title="删除该推广" href="javascript:wt.del_auto(' + p.product_id + ',' + p.campaign_id + ')">删除</a>';
                                $('#oper_' + p.product_id + '_' + p.campaign_id).html(oper_btns);
							break;
							
							case "0":
								status_td.html( '<span class="label label-info">正在等待中</span>' )  ;
								progress.css( {'width' : '0'} );
							break;
							
							case "1":
								status_td.html( '<span class="label label-info">正在智能分析中</span>' )  ;
								progress.css( {'width' : p.progress + '%'} );
							break;
							
							case "2":
								var text = ' <span class="label label-success">智能分析完成';
                                var oper_btns = '<a class="actionBtn" title="查看该推广" href="/adgroup/manage?adgroup_id=' + p.adgroup_id + '">查看</a>'

								if( p.need_confirm == '1' ){
								    text += ',等待确认提交';
									oper_btns += '&nbsp;<a class="actionBtn" title="确认该推广" href="javascript:wt.confirm_auto(' + p.product_id + ',' + p.campaign_id + ')">确认</a>';
									oper_btns += '&nbsp;<a class="actionBtn" title="删除该推广" href="javascript:wt.del_auto(' + p.product_id + ',' + p.campaign_id + ')">删除</a>';
								} else {
								    text += ', 已在推广中 ';
								}
								text += '</span>';
									
								status_td.html( text ) ;
								progress.css( {'width' : '100%'} );
								$('#oper_' + p.product_id + '_' + p.campaign_id).html(oper_btns);
							break;
						}
					}
					if(p.status < 0 || p.status > 1) {
						$tr.attr('rel', '');
					}
				}
				
			}
			
		});
	} , 3000);
	
	wt.confirm_auto = function(product_id, campaign_id) {
		var url = '/adgroup/autoconfirm?product_id=' + product_id + '&campaign_id=' + campaign_id;
		wt.get(url, '', function(r) {
			var o = $.parseJSON(r);
			var p = o.data.product;
		    if(o.status == 200) {
				//$oper_btns = '<a class="actionBtn" title="查看该推广" href="/adgroup/manage?adgroup_id=' + p.adgroup_id + '">查看</a>';
	            //.html(oper_btns);
	            var oper_btns = $('#oper_' + product_id + '_' + campaign_id).find('a');
	            $('#oper_' + product_id + '_' + campaign_id).html(oper_btns[0]);
		    }
		});
	};
	
	wt.del_auto = function(product_id, campaign_id) {
	    var url = '/adgroup/autoremove?product_id=' + product_id + '&campaign_id=' + campaign_id;
	    wt.showBox('确认删除该推广?', '确认', function(yes) {
			if(yes) {
				wt.get(url, '', function(r) {
					var o = $.parseJSON(r);
					if(o.status == 200 && o.data.result) {
						$('#product_' + product_id + '_' + campaign_id).fadeOut('slow' , function(){
		                    $(this).remove();
		                });
					}
		        });
			}
		});
    };
    
</script>
